package edu.fsu.cs.SHPE;

import java.io.IOException;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.graphics.drawable.Drawable;
import android.util.Log;

public class SpeakerContainer extends ArrayList<Speaker> {
	
	private static final long serialVersionUID = 63677265814147006L;
	private Speaker tmpSpeaker;
	private String tmpString;
	private final static String TAG = "SHPE.SpeakerContainer";
	
	public SpeakerContainer(String url) throws ConnectException{
		if(!parseXml(url))
			throw new ConnectException();
	}
	

	private boolean parseXml(String url){

		Log.i(TAG, "<" + url + ">");

		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();

			// Multiple conference testserver
			//"http://ww2.cs.fsu.edu/~sposaro/test/index.html"

			URL sourceUrl = new URL(url);
			ConferenceXMLHandler conferenceHandler = new ConferenceXMLHandler();
			xr.setContentHandler(conferenceHandler);
			xr.parse(new InputSource(sourceUrl.openStream()));	

		} catch (Exception e) {
			return false;
		}
		
		return true;

	}

	public class ConferenceXMLHandler extends DefaultHandler {


		public void startDocument() throws SAXException {
			Log.i(TAG, "Document processing started");

			tmpSpeaker = null;
		}


		public void endDocument() throws SAXException {
			Log.i(TAG, "Document processing finished");

			if(tmpSpeaker != null)
				add(tmpSpeaker);
		}


		public void startElement(String uri, String localName, String qName, Attributes attributes){
			Log.i(TAG, "in element " + localName + " qName " + qName + " uri " + uri);


			if(localName.equalsIgnoreCase("conference-speaker")){


				if(tmpSpeaker != null){
					add(tmpSpeaker);
				}


				tmpSpeaker = null;

				tmpSpeaker = new Speaker();

				if(attributes.getValue("xlink:photo") != null){
					URL src;
					try {
						src = new URL(attributes.getValue("xlink:photo"));
						tmpSpeaker.setPhoto(Drawable.createFromStream(src.openStream(), "photo"));
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				tmpSpeaker.setId(Long.parseLong(attributes.getValue("id")));

			}

			if(localName.equalsIgnoreCase("name")){
				tmpSpeaker.setFirstName(attributes.getValue("first"));
				tmpSpeaker.setLastName(attributes.getValue("last"));
			}
		}

		public void characters(char[] ch, int start, int length) throws SAXException{
			tmpString = new String(ch, start, length);
			Log.i(TAG, "string " + tmpString);
		}

		public void endElement(String uri, String localName, String qName)
		throws SAXException {
			Log.i(TAG, "endElement " + localName);

			if(localName == null || tmpString == null || tmpSpeaker == null)
				return;

			if(localName.equalsIgnoreCase("title"))
				tmpSpeaker.setTitle(tmpString);
			else if(localName.equalsIgnoreCase("email"))
				tmpSpeaker.setEmail(tmpString);
			else if(localName.equalsIgnoreCase("website"))
				tmpSpeaker.setWebsite(tmpString);
			else if(localName.equalsIgnoreCase("organizationOrCompany"))
				tmpSpeaker.setOrganization(tmpString);
			else if(localName.equalsIgnoreCase("linkedIn"))
				tmpSpeaker.setLinkedIn(tmpString);
			else if(localName.equalsIgnoreCase("biography"))
				tmpSpeaker.setBiography(tmpString);
			else if(localName.equalsIgnoreCase("facebook"))
				tmpSpeaker.setFacebook(tmpString);
		}

	}

}
